package com.smartcommunity.controller.common;

import com.alibaba.fastjson.JSONObject;
import com.smartcommunity.controller.BaseController;
import com.smartcommunity.entity.system.dto.AddSysDto;
import com.smartcommunity.entity.system.dto.UpdateSysDto;
import com.smartcommunity.log.interceptor.Log;
import com.smartcommunity.service.common.SystemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: JiangM
 * @Date: 2018/4/16
 * @Description: 系统参数控制层
 * @Modified By:
 */
@Api(value = "系统参数SysController", tags = {"系统参数"})
@RestController
@RequestMapping("/system")
public class SysController extends BaseController {

    @Autowired
    private SystemService systemService;

    /**
     * 系统参数
     *
     * @return
     */
    @ApiOperation(value = "获取所有系统参数", notes = "获取所有系统参数，没有分页", httpMethod = "GET", produces = "application/json")
    @RequestMapping(value = "/listSysParams", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public List<JSONObject> listSysParams() {
        return systemService.getSysParams();
    }

    /**
     * 更新系统参数
     */
    @ApiOperation(value = "更新系统参数", notes = "更新系统参数", httpMethod = "POST", produces = "application/json")
    @Log(info = "更新系统参数",module = "系统配置",function = "更新")
    @RequestMapping(value = "/updateSysParam", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public JSONObject updateSysParam(@Valid @RequestBody UpdateSysDto updateSysDto) {

        Map<String, Object> paramMap = new HashMap<>(2);
        paramMap.put("key", updateSysDto.getKey());
        paramMap.put("value", updateSysDto.getValue());

        return systemService.updateSysParam(paramMap);
    }

    /**
     * 新增系统参数
     */
    @ApiOperation(value = "新增系统参数", notes = "新增系统参数", httpMethod = "POST", produces = "application/json")
    @Log(info = "新增系统参数",module = "系统配置",function = "新增")
    @RequestMapping(value = "/addSysParam", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
    public JSONObject addSysParam(@Valid @RequestBody AddSysDto addSysDto) {

        Map<String, Object> paramMap = new HashMap<>(2);
        paramMap.put("key", addSysDto.getKey());
        paramMap.put("value", addSysDto.getValue());
        paramMap.put("name", addSysDto.getName());
        return systemService.addSysParam(paramMap);
    }

    public static void main(String[] args) {
        Date date = new Date(1577822400000L);
        System.out.println(date);
    }

}
